﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WindowsForms.AOnline.Presenter.Dictionary.Account;
using WindowsForms.AOnline.Presenter.Dictionary.InventoryItem;
using WindowsForms.AOnline.View.Dictionary;
using WindowsForms.AOnline.WindowsForm.FormBase;
using WindowsForms.AOnline.WindowsForm.FormBase.PropertyGrid;

namespace WindowsForms.AOnline.WindowsForm.FormDictionary
{
    public partial class FrmXtraInventoryItemDetail : FrmXtraBaseCategoryDetail, IInventoryItemView, IAccountsView  
    {

        private readonly InventoryItemPresenter _inventoryItemPresenter;
        private readonly AccountsPresenter _accountsPresenter;

        public FrmXtraInventoryItemDetail()
        {
            InitializeComponent();
            _inventoryItemPresenter = new InventoryItemPresenter(this);
            _accountsPresenter = new AccountsPresenter(this);
        }
        #region Overrides Function

        /// <summary>
        /// Initializes the data.
        /// </summary>
        protected override void InitData()
        {
            _accountsPresenter.DisplayInventoryItem();
            if (KeyValue != null)
                _inventoryItemPresenter.Display(KeyValue);
        }

        /// <summary>
        /// Saves the data.
        /// </summary>
        /// <returns></returns>
        protected override string SaveData()
        {
            return _inventoryItemPresenter.Save();
        }

        #endregion
        public long InventoryItemId
        {
            get;

            set;
        }

        public string InventoryItemCode
        {
            get { return txtInventoryItemCode.Text; }
            set { txtInventoryItemCode.Text = value; }
        }

        public string InventoryItemName
        {
            get { return txtInventoryItemName.Text; }
            set { txtInventoryItemName.Text = value; }
        }

        public string AccountCode
        {
            get
            {

                return (string)cboAccountCode.GetColumnValue("AccountCode");
            }
            set
            {
                cboAccountCode.EditValue = value;
            }
        }

        public string Unit
        {
            get { return txtUnit.Text; }
            set { txtUnit.Text = value; }
        }

        public bool IsActive
        {
            get { return chkIsActive.Checked; }
            set { chkIsActive.Checked = value; }
        }

        public long UnitId
        {
            get;

            set;
        }

        #region Combobox
        public IList<Model.BusinessObjects.Dictionary.AccountModel> Accounts
        {
            set
            {
                if (value == null) return;
                cboAccountCode.Properties.DataSource = value;
                cboAccountCode.Properties.PopulateColumns();
                var gridColumnsCollection = new List<XtraColumn> { 
                    new XtraColumn { ColumnName = "AccountCode", ColumnCaption = "Mã tài khoản", ColumnPosition = 1, ColumnVisible = true, ColumnWith = 200 },
                    new XtraColumn { ColumnName = "AccountName", ColumnCaption = "Tên tài khoản", ColumnPosition = 2, ColumnVisible = true, ColumnWith = 300 },
                    new XtraColumn { ColumnName = "AccountId",  ColumnVisible = false, },
                    new XtraColumn { ColumnName = "ParentId",  ColumnPosition = 3, ColumnVisible = false },
                    new XtraColumn { ColumnName = "Description", ColumnVisible = false, },
                    new XtraColumn { ColumnName = "IsActive", ColumnVisible = false },
                    new XtraColumn { ColumnName = "Grade", ColumnVisible = false },
                    new XtraColumn { ColumnName = "IsDetail", ColumnVisible = false },
                    new XtraColumn { ColumnName = "UnitId", ColumnVisible = false },
                    new XtraColumn { ColumnName = "IsCustomer", ColumnVisible = false },
                    new XtraColumn { ColumnName = "IsVendor", ColumnVisible = false },
                    new XtraColumn { ColumnName = "IsInventoryItem", ColumnVisible = false },                 
                    new XtraColumn { ColumnName = "IsFixedAsset", ColumnVisible = false },
                    new XtraColumn { ColumnName = "IsTax", ColumnVisible = false },
                    new XtraColumn { ColumnName = "IsSystem", ColumnVisible = false }
            };

                foreach (var column in gridColumnsCollection)
                {
                    if (column.ColumnVisible)
                    {
                        cboAccountCode.Properties.Columns[column.ColumnName].Caption = column.ColumnCaption;
                        cboAccountCode.Properties.SortColumnIndex = column.ColumnPosition;
                        cboAccountCode.Properties.Columns[column.ColumnName].Width = column.ColumnWith;
                    }
                    else { cboAccountCode.Properties.Columns[column.ColumnName].Visible = false; }
                }
                cboAccountCode.Properties.DisplayMember = "AccountCode";
                cboAccountCode.Properties.ValueMember = "AccountCode";
            }
        }
        #endregion
    }
}